package cn.kgc.mes.storage.controller;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.kgc.mes.common.annotation.Log;
import cn.kgc.mes.common.core.controller.BaseController;
import cn.kgc.mes.common.core.domain.AjaxResult;
import cn.kgc.mes.common.enums.BusinessType;
import cn.kgc.mes.storage.domain.StorageLocation;
import cn.kgc.mes.storage.service.IStorageLocationService;
import cn.kgc.mes.common.utils.poi.ExcelUtil;
import cn.kgc.mes.common.core.page.TableDataInfo;

/**
 * 库区Controller
 * 
 * @author ydw
 * @date 2025-09-12
 */
@RestController
@RequestMapping("/location/location")
public class StorageLocationController extends BaseController
{
    @Autowired
    private IStorageLocationService storageLocationService;

    /**
     * 查询库区列表
     */
//    @PreAuthorize("@ss.hasPermi('location:location:list')")
    @GetMapping("/list")
    public TableDataInfo list(StorageLocation storageLocation)
    {
        startPage();
        List<StorageLocation> list = storageLocationService.selectStorageLocationList(storageLocation);
        return getDataTable(list);
    }

    /**
     * 导出库区列表
     */
//    @PreAuthorize("@ss.hasPermi('location:location:export')")
    @Log(title = "库区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StorageLocation storageLocation)
    {
        List<StorageLocation> list = storageLocationService.selectStorageLocationList(storageLocation);
        ExcelUtil<StorageLocation> util = new ExcelUtil<StorageLocation>(StorageLocation.class);
        util.exportExcel(response, list, "库区数据");
    }

    /**
     * 获取库区详细信息
     */
//    @PreAuthorize("@ss.hasPermi('location:location:query')")
    @GetMapping(value = "/{locationId}")
    public AjaxResult getInfo(@PathVariable("locationId") Long locationId)
    {
        return success(storageLocationService.selectStorageLocationByLocationId(locationId));
    }

    /**
     * 新增库区
     */
//    @PreAuthorize("@ss.hasPermi('location:location:add')")
    @Log(title = "库区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StorageLocation storageLocation)
    {
        return toAjax(storageLocationService.insertStorageLocation(storageLocation));
    }

    /**
     * 修改库区
     */
//    @PreAuthorize("@ss.hasPermi('location:location:edit')")
    @Log(title = "库区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StorageLocation storageLocation)
    {
        return toAjax(storageLocationService.updateStorageLocation(storageLocation));
    }

    /**
     * 删除库区
     */
//    @PreAuthorize("@ss.hasPermi('location:location:remove')")
    @Log(title = "库区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{locationIds}")
    public AjaxResult remove(@PathVariable Long[] locationIds)
    {
        return toAjax(storageLocationService.deleteStorageLocationByLocationIds(locationIds));
    }

    //修改库区是否启用
    @PostMapping("/updatenew")
    public Object updata1(@RequestBody StorageLocation storageLocation){
        boolean result=storageLocationService.updateStorageLocationIsEnable(storageLocation);
        if(result){
            return "success";
        }else{
            return "error";
        }
    }
}
